<?php
/**
 * Created by PhpStorm.
 * User: 1
 * Date: 2017-03-17
 * Time: 15:37
 */

namespace app\src\sunsun\cp1000\action;

use app\src\sunsun\cp1000\logic\Cp1000DeviceEventLogic;
use app\src\sunsun\cp1000\model\Cp1000DeviceEvent;
use sunsun\cp1000\req\Cp1000DeviceEventReq;


/**
 * Class Cp1000DeviceInfoAction
 * tcp 客户端通用操作
 * @package app\src\sunsun\cp1000
 */
class Cp1000DeviceEventAction extends Cp1000BaseAction
{

    /**
     * 清除过期数据
     * @param $dataTimestamp
     * @return array
     */
    public function clearExpiredData($dataTimestamp)
    {
        $map['create_time'] = ['lt', $dataTimestamp];
        $map['pro_status'] = ['gt', 0];//已处理的才删除
        $result = (new Cp1000DeviceEventLogic())->delete($map);
        return $result;
    }

    public function add($did, $eventType, $gear, $mode)
    {
        $req = new Cp1000DeviceEventReq();
        $req->setCode($eventType);
        $req->setMode($mode);
        $req->setGear($gear);
        $eventInfo = json_encode(['mode' => $req->getMode(), 'gear' => $req->getGear()]);
        $data = [
            'did' => $did,
            'event_type' => $eventType,
            'event_info' => $eventInfo,
            'create_time' => time(),
            'update_time' => time(),
            'pro_status' => Cp1000DeviceEvent::PRO_STATUS_NOT_PROCESS
        ];
        $result = (new Cp1000DeviceEventLogic())->add($data);
        return $result;
    }

}